package com.hns.can.mainte.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hns.can.base.entity.SysCodeDscEntity;
import com.hns.can.mainte.entity.AppCarMaintenContentEntity;
import com.hns.can.mainte.entity.AppCarMaintenItemInfoEntity;
import com.hns.can.mainte.vo.MaintenanceVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.mainte.entity.AppCarMaintenRmndrInfoEntity;
import com.hns.can.mainte.entity.AppPartReplaceSetEntity;
import com.hns.can.mainte.vo.AppPartsReplaceSetParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 维保设置
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Service
public class FunMaintenaSetService extends CommonService {
	@Autowired
    private FillCodeDscValue codeDscValue;
    /**
     * 查询维保计划设置信息
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public AppCarMaintenRmndrInfoEntity getMaintenRmndrSetInfo(Map<String, Object> map) throws Exception{
        List<AppCarMaintenRmndrInfoEntity> list = PubUtil.convertListMapToObject(
            this.sqlMapDao.selectList("funMaintenaSet.getMaintenRmndrSetInfo", map), AppCarMaintenRmndrInfoEntity.class);
        if(PubUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
    /**
     *删除维保计划设置信息
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void delMaintenRmndrSetInfo(Map<String, Object> map) throws Exception{
    	sqlMapDao.delete("funMaintenaSet.delMaintenRmndrSetInfo",map);
    }
    /**
     *删除零部件计划设置信息
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void delPartsReplaceSetInfo(Map<String, Object> map) throws Exception{
    	sqlMapDao.delete("funMaintenaSet.delPartsReplaceSetInfo",map);
    }
    /**
     * 维保信息列表
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public Pagination getMaintenRmndrSetInfoList(Map<String, Object> map) throws Exception{
    	Pagination page = this.myBatisPage("funMaintenaSet.getMaintenRmndrSetCnt", "funMaintenaSet.getMaintenRmndrSetInfo", map, AppCarMaintenRmndrInfoEntity.class);
    	List list =page.getData();
    	Map<String,String> mapValue = new HashMap<String,String>();
    	mapValue.put("carType", "CAT00000");
    	mapValue.put("mntType", "MAIN1000");
        codeDscValue.fillCodeDscValue(list, mapValue);
    	return page;
    }
    
    /**
     * 零部件计划列表
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
	public Pagination getPartsReplaceSetInfoList(Map<String, Object> map) throws Exception{
    	Pagination page = this.myBatisPage("funMaintenaSet.getPartsReplaceSetCnt", "funMaintenaSet.getPartsReplaceSetInfo", map, AppPartReplaceSetEntity.class);
    	List list =page.getData();
    	if(PubUtil.isNotEmpty(list)){
	    	for(Object appPartReplaceSetEntity:list){
	    		AppPartReplaceSetEntity entity = (AppPartReplaceSetEntity) appPartReplaceSetEntity;
	    		BeanUtils.copyProperty(entity,  "carTypeName", entity.getCarType());
	    	}
    	}
    	Map<String,String> mapValue = new HashMap<String,String>();
    	mapValue.put("carTypeName", "CAT00000");
    	codeDscValue.fillCodeDscValue(list, mapValue);
    	return page;
    }
    
    /**
     * 零部件计划列表
     * @param map
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
	@Transactional
	public List<AppPartReplaceSetEntity> getPartsReplaceSetInfo(Map<String, Object> map) throws Exception {
    	List<AppPartReplaceSetEntity> appCarMaintenInfoList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("funMaintenaSet.getPartsReplaceSetInfo", map), AppPartReplaceSetEntity.class);
        return appCarMaintenInfoList;
    }
    /**
     * 更新维保计划
     * @param rmndrInfo
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void uptMaintenRmndrSetInfo(AppCarMaintenRmndrInfoEntity rmndrInfo) throws Exception {
    	AppCarMaintenRmndrInfoEntity dbRmndrInfo=commonEntityDao.find(rmndrInfo.getRcrdId(), AppCarMaintenRmndrInfoEntity.class);
        if(PubUtil.isNotEmpty(dbRmndrInfo)){//是否存在维保计划
            PubUtil.copyPropertiesInclude(dbRmndrInfo, rmndrInfo, new String[]{"corpId","crtTime","crtUser"});
            this.commonEntityDao.update(rmndrInfo);
        }
    }
    
    /**
     * 保存维保计划
     * @param rmndrInfo
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void addMaintenRmndrSetInfo(AppCarMaintenRmndrInfoEntity rmndrInfo,SysUserEntity user ) throws Exception {
    	// 后台校验
    	AppCarMaintenRmndrInfoEntity entity =new AppCarMaintenRmndrInfoEntity();
    	entity.setCorpId(user.getCorpId());
    	entity.setCarType(rmndrInfo.getCarType());
    	entity.setMntType(rmndrInfo.getMntType());
    	checkAddAndUpdate(entity);
    	String id = user.getAcctName();
        rmndrInfo.setRcrdId(UtilString.getUUID());
        rmndrInfo.setCorpId(user.getCorpId());
        rmndrInfo.setCrtTime(new Date());
        rmndrInfo.setCrtUser(id);
        rmndrInfo.setUpdateTime(new Date());
        rmndrInfo.setUpdateUser(id);
	    commonEntityDao.save(rmndrInfo);
    }
    
    /**
     * 保存零部件计划
     * @param appPartsReplaceSetParamVo 零部件计划参数
     * @param user 
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void savePartsReplaceSetInfo(AppPartsReplaceSetParamVo appPartsReplaceSetParamVo,SysUserEntity user ) throws Exception {
        List<String> partsCodeList=appPartsReplaceSetParamVo.getPartsCode();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("carType", appPartsReplaceSetParamVo.getCarType());
        map.put("mntMile",appPartsReplaceSetParamVo.getMntMile());
        //删除零部件计划
        delPartsReplaceSetInfo(map);
        //新增零部件计划
        for(String partCode:partsCodeList){
        	AppPartReplaceSetEntity appPartsReplaceSetEntity=new AppPartReplaceSetEntity();
            String id = user.getAcctName();
            appPartsReplaceSetEntity.setCorpId(user.getCorpId());
            appPartsReplaceSetEntity.setCrtTime(new Date());
            appPartsReplaceSetEntity.setCrtUser(id);
            appPartsReplaceSetEntity.setUpdateTime(new Date());
            appPartsReplaceSetEntity.setUpdateUser(id);
            appPartsReplaceSetEntity.setMntMile(appPartsReplaceSetParamVo.getMntMile());
            appPartsReplaceSetEntity.setCarType(appPartsReplaceSetParamVo.getCarType());
        	appPartsReplaceSetEntity.setRcrd_id(UtilString.getUUID());
        	appPartsReplaceSetEntity.setPartsCode(partCode);
        	commonEntityDao.save(appPartsReplaceSetEntity);
        }
    }
    
    /**
     * 检查添加和更新是否符合条件，后台校验
     * @param entity
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
	private void checkAddAndUpdate(AppCarMaintenRmndrInfoEntity entity) throws Exception{
    	 List<AppCarMaintenRmndrInfoEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("funMaintenaSet.getMaintenRmndrSetInfo", entity), AppCarMaintenRmndrInfoEntity.class);
    	 if(PubUtil.isNotEmpty(list)&&list.size()>0){
    		 throw new BusinessException(MsgType.WARN.getFlag(),"该车型与维保类型已添加！");
    	 }
    }

    /**
     * 获取企业对应维保项目
     * @author zhangy
     * @version Revision 2.0.0
     * @email: zhangy2222z@sina.cn
     * @create 2017-04-19 19:53
     **/
    public List getMaintenaItemInfo(String corpId) {
        HashMap map = new HashMap();
        map.put("corpId", corpId);
        List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("funMaintenaSet.getMaintenaItemInfo", map), AppCarMaintenItemInfoEntity.class);
        return list;

    }

    /**
     * 编辑企业对应维保项目
     * @author zhangy
     * @version Revision 2.0.0
     * @email: zhangy2222z@sina.cn
     * @create 2017-04-19 19:53
     **/
    @Transactional
    public void uptMaintenaSetInfo(MaintenanceVo vo, String userId) {
        this.sqlMapDao.delete("funMaintenaSet.delMaintenaSetInfo", vo);
        for(int i=0; i < vo.getMaintenanceItemNames().size(); i++){
            AppCarMaintenItemInfoEntity entity = new AppCarMaintenItemInfoEntity();
            if("-1".equals(vo.getIds().get(i))){
                entity.setId(UtilString.getUUID());
            }else{
                entity.setId(vo.getIds().get(i));
            }
            entity.setCorpId(vo.getCorpId());
            entity.setMaintenItemName(vo.getMaintenanceItemNames().get(i));
            if(PubUtil.isNotEmpty(vo.getRemarks()) && i < vo.getRemarks().size()){
                entity.setRemark(vo.getRemarks().get(i));
            }
            entity.setCrtTime(new Date());
            entity.setCrtUser(userId);
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(userId);
            this.commonEntityDao.save(entity);
        }
        //TODO 删除维保计划对应的维保项目，并保存新的

    }

    public List getMaintenancePart() {
        List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("funMaintenaSet.getMaintenancePart"), SysCodeDscEntity.class);
        return list;
    }

    public List getMaintenaContentInfo(MaintenanceVo vo) {
        List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("funMaintenaSet.getMaintenaContentInfo"), AppCarMaintenContentEntity.class);
        return list;
    }

    @Transactional
    public void uptMaintenaContent(MaintenanceVo vo, String userId) {
        this.sqlMapDao.delete("funMaintenaSet.delMaintenaContent", vo);
        for(int i=0; i < vo.getMaintenanceItemNames().size(); i++){
            AppCarMaintenContentEntity entity = new AppCarMaintenContentEntity();
            if("-1".equals(vo.getIds().get(i))){
                entity.setId(UtilString.getUUID());
            }else{
                entity.setId(vo.getIds().get(i));
            }
            entity.setPartCode(vo.getPartCode());
            entity.setCorpId(vo.getCorpId());
            entity.setMaintenContent(vo.getMaintenanceContentNames().get(i));
            if(PubUtil.isNotEmpty(vo.getRemarks()) && i < vo.getRemarks().size()){
                entity.setRemark(vo.getRemarks().get(i));
            }
            entity.setCrtTime(new Date());
            entity.setCrtUser(userId);
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(userId);
            this.commonEntityDao.save(entity);
        }

    }
}
